---php---
<?php

namespace co2_OS;

$boot=__DIR__.'/kernel/boot'; # where boot files are

define('sysvar', __DIR__.'/kernel/conf/sysvar'); # where settings file is

/*-- begin distribution --*/

define('co2_OS\cli_dir',getcwd());
define('co2_OS\cwd',getcwd());

foreach(scandir($boot) as $file)
if($file!='.'&&$file!='..')

{
	if(preg_match('/^function\.|class\.|constants\./',$file))
	require_once "$boot/$file";
}

spl_autoload_register('co2_OS\autoload');


new sysvar;




require_once sysvar::get('kernel').'/api/'.sysvar::get('api_channel').'/'.sysvar::get('api').'/co2.php';

use \co2\dev\v0_0_0 as api;

new api\runtime((object)array(
	'properties'=>
	array
	(
		'catch php errors'		=>	true
	),
	'error_handlers'=>
	array
	(
		function($level,$message,$file,$line,$context)
		{
			throw new api\Exception($message,null,$level,$file,$line,$context);
		}
	)
));

foreach(scandir($boot) as $file)
if($file!='.'&&$file!='..')

{
	if(preg_match('/^api\./',$file))
	require_once "$boot/$file";
}








$verbose=false;
$run_channel='stable';

if(!isset($argv[1]))
quit(EXIT_STATUS::USAGE,' * Usage: co2 { install | uninstall | [run] $app }');

$A=$argv; array_shift($A);

$ARGS=parse_args($A);

if(preg_match('/^-/',$ARGS[0]))

{
  if(preg_match('/v/',$ARGS[0]))
  $verbose=true;
  
  if(preg_match('/d/',$ARGS[0]))
  $run_channel='dev';
  
  array_shift($ARGS);
}

define('co2_verbose',$verbose);
	
try

{
	try
	{
		switch($action=$ARGS[0])
	
		{
			default:
		
		    $app=new app(strtolower($ARGS[0]),null,$run_channel);
				
#				$run=run(strtolower($ARGS[0]),null,$run_channel);
				
				if(isset($ARGS[1]))
				
				{
				  $_ARGS=$ARGS;
          
          array_shift($_ARGS);
          
				  $method=array_shift($_ARGS);

#				  $run2=call_user_func_array(array($run,$method),$_ARGS);
          $command=$app->command($cmd,$_ARGS);
				  
				  if(method_exists($run2,'__toString'))
				  quit(EXIT_STATUS::APP_SAYS,(string)$run2);
				}
        else
#        quit(EXIT_STATUS::README,$run());
#        `more {$run('FILE')} < \`tty\` > \`tty\``;
        echo $app->nutshell;
		
			break;
			
			case'install':
			
			  if(!isset($ARGS[1]))
			  quit(EXIT_STATUS::MISSING_ARGUMENTS,'Please specify a package type');
			  
			  $type=$ARGS[1];
			  
			  if(!in_array($type,array('app','lib')))
			  quit(EXIT_STATUS::UNKNOWN_PACKAGE_TYPE,'Unknown package type');
			  
			  if(!isset($ARGS[2]))
			  quit(EXIT_STATUS::MISSING_ARGUMENTS,'Please enter package name');
			  
			  $pkg=strtolower($ARGS[2]);
			  
			  $channel='stable';
			  $version=null;
			  
			  if(isset($ARGS[3])&&is_object($ARGS[3]))
			  
			  {
			    if(isset($ARGS[3]->version))
			    $version=$ARGS[3]->version;
			    
			    if(isset($ARGS[3]->channel))
			    $channel=$ARGS[3]->channel;
			  }
			 
			  $function="co2_OS\\install_$type";
			  
			  $function($pkg,$version,$channel);
			  
			  quit(EXIT_STATUS::PKG_SUCCESSFULLY_INSTALLED,$type.' successfully installed');
		
			break;
			
			case'uninstall':
			
			  if(!isset($ARGS[1]))
			  quit(EXIT_STATUS::MISSING_ARGUMENTS,'Please specify a package type');
			  
			  $type=$ARGS[1];
			  
			  if(!in_array($type,array('app','lib')))
			  quit(EXIT_STATUS::UNKNOWN_PACKAGE_TYPE,'Unknown package type');
			  
			  if(!isset($ARGS[2]))
			  quit(EXIT_STATUS::MISSING_ARGUMENTS,'Please enter package name');
			  
			  $pkg=strtolower($ARGS[2]);
			  
			  $channel='stable';
			  $version=null;
			  $clean=false;
			  
			  if(isset($ARGS[3])&&is_object($ARGS[3]))
			  
			  {
			    if(isset($ARGS[3]->version))
			    $version=$ARGS[3]->version;
			    
			    if(isset($ARGS[3]->channel))
			    $channel=$ARGS[3]->channel;
			    
			    if(isset($ARGS[3]->junkies))
			    $clean=$ARGS[3]->junkies;
			  }
			 
			  $function="co2_OS\\uninstall_$type";
			  
			  $function($pkg,$version,$channel,$clean);
			  
			  quit(EXIT_STATUS::PKG_SUCCESSFULLY_INSTALLED,$type.' successfully uninstalled');
			
			break;
		}
	}
	catch(api\Exception $e)
	
	{
		quit($e->getLevel(),$e->getMessage().'
'.$e->getFile().'@'.$e->getLine(),(object)array('trace'=>$e->getTraceAsString(),'type'=>'xml'));
	}
}

catch(\Exception $e)

{
	quit(EXIT_STATUS::EXCEPTION,$e->getMessage().'
'.$e->getFile().'@'.$e->getLine(),(object)array('trace'=>$e->getTraceAsString(),'type'=>'xml'));
}
